//
//  STDSOpenGL.h
//  STDRenderSDK
//
//  Created by 陈学明 on 2023/3/12.
//

#ifndef STDSOpenGL_h
#define STDSOpenGL_h

#include <stdio.h>
#include <functional>
#include <string>
#include <map>
#include <memory>
#include <vector>

#include "STDSLog.h"
#ifdef __ANDROID__

#include <EGL/egl.h>
#include <GLES3/gl3.h>
#include <GLES3/gl3ext.h>
#define GL_WRITE_ONLY GL_WRITE_ONLY_OES
#define GL_DEPTH24_STENCIL8 GL_DEPTH24_STENCIL8_OES
#define glClearDepth glClearDepthf
#define OPENGL_ES
#define GP_USE_VAO

// Define  __APPLE__  IOS
#elif defined(__APPLE__)

#include <OpenGLES/ES3/gl.h>
#include <OpenGLES/ES3/glext.h>
#define glBindVertexArray glBindVertexArrayOES
#define glDeleteVertexArrays glDeleteVertexArraysOES
#define glGenVertexArrays glGenVertexArraysOES
#define glIsVertexArray glIsVertexArrayOES
#define glMapBuffer glMapBufferOES
#define glUnmapBuffer glUnmapBufferOES
#define GL_WRITE_ONLY GL_WRITE_ONLY_OES
#define GL_DEPTH24_STENCIL8 GL_DEPTH24_STENCIL8_OES
#define glClearDepth glClearDepthf
#define OPENGL_ES
#define GP_USE_VAO

#endif

#define GXNS_GL(x) x


#define TEXTURE_OES     (GXNS_GL(GLenum))0x8D65
#define TEXTURE_NON     0
#define HANDLE_NON      0

#define SAFE_DELETE(x) \
do {\
    if (x != nullptr) {\
        delete x;\
        x = nullptr;\
    }\
} while (0)

#define SAFE_DELETE_ARRAY(x) \
do {\
    if (x != nullptr) {\
        delete []x;\
        x = nullptr;\
    }\
} while (0)

#ifdef __ANDROID__
#define SHDR_PRECISION_NORM precision mediump float;
#else
#define SHDR_PRECISION_NORM precision highp float;
#endif

#define SHDR_VERT_HEAD_NORM \
    attribute vec4 aPos; \
    attribute vec2 aTexCoord; \
    varying vec4 vPosition; \
    varying vec2 vTexCoord;

#define SHDR_VERT_COLOR_HEAD \
    attribute vec4 aPos; \
    attribute vec4 aColor; \
    varying vec4 vPosition; \
    varying vec4 vColor;

#define SHDR_FRAG_COLOR_HEAD(precise) \
    precision precise float; \
    varying vec4 vPosition; \
    varying vec4 vColor;

#define SHDR_FRAG_HEAD(precise) \
    precision precise float; \
    varying vec4 vPosition; \
    varying vec2 vTexCoord;

#define SHDR_FRAG_HEAD_NORM \
    SHDR_PRECISION_NORM \
    varying vec4 vPosition; \
    varying vec2 vTexCoord;

#define SHDR_FRAG_HEAD_BLEND \
    SHDR_PRECISION_NORM \
    varying vec4 vPosition;\
    varying vec2 vTexCoord;

#define SHDR_TEXTURE2D texture2D
#define SHDR_TEXTURE1D texture1D
#define SHDR_VERT_ATTRIBUTE attribute
#define SHDR_VERT_VARYING varying
#define SHDR_FRAG_VARYING varying
#define SHDR_OUTCOLOR gl_FragColor = outColor;

#define _SHADER_SRC(...) #__VA_ARGS__
#define SHADER_SRC(...) _SHADER_SRC(__VA_ARGS__)
#endif /* OpenGL_h */
